<?php

namespace App\Model;

use Trois\Lib\Help;

class BizModel extends BaseModel
{
    protected $tableName = 'by_crm_business';
    protected $autoTimeStamp = false;
    protected string $primaryKey = 'biz_id';
    protected array $conditionCacheFiled = [];
    protected bool $hasIsDeleteField = false;
    protected bool $hasStatueField = false;

    public function handleAllCache(int $group_id): array
    {
        $all = $this
            ->field('biz_id as id,biz_name as name,biz_logo as logo,biz_status as status,mange_brands')
            ->where(['group_id' => $group_id])
            ->order('sort', 'ASC')
            ->all();

        $all = $all ? $all->toArray(false, false) : [];

        Help::redisPoolStringInvoke('all_cache:' . $group_id . ':' . $this->tableName, json_encode($all, JSON_UNESCAPED_UNICODE), 0);

        return $all;

    }


    public function getAllCache(int $group_id): array
    {
        $all = Help::redisPoolStringInvoke('all_cache:' . $group_id . ':' . $this->tableName);

        $all = $all ? json_decode($all, true) : [];

        if (!$all) {
            $all = $this->handleAllCache($group_id);
        }

        return $all ?: [];
    }

}